package com.dodo.blog.ui.ajax;

import com.dodo.blog.RequestCycle;
import com.dodo.blog.ui.message.Message;
import com.dodo.blog.ui.message.MessageLevel;
import com.dodo.blog.ui.page.Page;

import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
public class Response
{
    public enum ResponseType
    {
        REDIRECT
    }

    private String value;

    private String responseType;

    private List<Message> messages;

    private Object model;

    private boolean hasErrors;

    public Response()
    {
    }

    public Response( String responseType, String value )
    {
        this.value = value;
        this.responseType = responseType;
    }

    public static Response redirect( String where )
    {
        return new Response( ResponseType.REDIRECT.name(), where );
    }

    public static Response redirect( Class<? extends Page> where )
    {
        return new Response( ResponseType.REDIRECT.name(), RequestCycle.get().getWebApplication().getPathByPageClass( where ) );
    }

    public static Response error( Set<Message> errors )
    {
        RequestCycle.get().addMessages( errors );
        Response response = new Response();
        response.setMessages( RequestCycle.get().getMessages() );

        return response;
    }

    public void setValue( String value )
    {
        this.value = value;
    }

    public String getValue()
    {
        return value;
    }

    public String getResponseType()
    {
        return responseType;
    }

    public void setMessages( List<Message> messages )
    {
        for ( Message message : messages )
        {
            if ( message.getLevel() == MessageLevel.ERROR )
            {
                hasErrors = true;
            }
        }
        this.messages = messages;
    }

    public boolean isHasErrors()
    {
        return hasErrors;
    }

    public List<Message> getMessages()
    {
        return messages;
    }

    public void setModel( Object model )
    {
        this.model = model;
    }

    public Object getModel()
    {
        return model;
    }
}